/** UserDao */
package server.service.user;

import java.util.ArrayList;
import java.util.List;

import server.Database;
import domain.user.SystemUser;
import domain.user.User;

/**
 * talks to the database about users
 * @author jessienewman
 * @version $Revision: 1.0 $
 */
public class UserDao {

	/**
	 * Method isValidUsername.
	 * @param username String
	
	 * @return Boolean */
	public Boolean thisIsValidUsername(String username) {
		List<User> users = Database.DATABASE.users;
		for (User u : users) {
			if (u.getUsername().equals(username)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Method loginValid.
	 * @param username String
	 * @param password String
	
	 * @return Boolean */
	public Boolean loginValid(String username, String password) {
		for (User u : Database.DATABASE.users) {
			if (u.getUsername().equals(username)) {
				if (u.getPassword().equals(password)) {
					return true;
				}
				return false;
			}
		}
		return false;
	}

	/**
	 * Method getUser.
	 * @param username String
	
	 * @return User */
	public User getUser(String username) {
		for (User u : Database.DATABASE.users) {
			if (u.getUsername().equals(username)) {
				return u;
			}
		}
		return null;
	}

	/**
	 * Method getUser.
	 * @param userId Integer
	
	 * @return User */
	public User getUser(Integer userId) {
		for (User u : Database.DATABASE.users) {
			if (u.getUserId() == userId) {
				return u;
			}
		}
		return null;
	}

	/**
	 * Method getUserId.
	 * @param username String
	
	 * @return Integer */
	public Integer getUserId(String username) {
		User user = getUser(username);
		return (user != null) ? user.getUserId() : -1;
	}

	/**
	 * Method modifyUsername.
	 * @param userId Integer
	 * @param username String
	 */
	public void modifyUsername(Integer userId, String username) {
		getUser(userId).setUsername(username);
		save();
	}

	/**
	 * Method modifyPassword.
	 * @param userId Integer
	 * @param password String
	 */
	public void modifyPassword(Integer userId, String password) {
		getUser(userId).setPassword(password);
		save();
	}

	/**
	 * Method addUser.
	 * @param u User
	
	 * @return Integer */
	public Integer addUser(User u) {
		return Database.DATABASE.addUser(u, u.getUsername(), u.getPassword());
	}

	/**
	 * Method modifyNameFirst.
	 * @param id Integer
	 * @param nameFirst String
	 */
	public void modifyNameFirst(Integer id, String nameFirst) {
		SystemUser user = (SystemUser) getUser(id);
		user.setNameFirst(nameFirst);
		save();
	}

	/**
	 * Method modifyNameMiddle.
	 * @param id Integer
	 * @param nameMiddle String
	 */
	public void modifyNameMiddle(Integer id, String nameMiddle) {
		SystemUser user = (SystemUser) getUser(id);
		user.setNameMiddle(nameMiddle);
		save();
	}

	/**
	 * Method modifyNameLast.
	 * @param id Integer
	 * @param nameLast String
	 */
	public void modifyNameLast(Integer id, String nameLast) {
		SystemUser user = (SystemUser) getUser(id);
		user.setNameLast(nameLast);
		save();
	}

	/**
	 * Method findUsers.
	 * @param s String
	
	 * @return List */
	public List<User> findUsers(String s) {
		List<User> users = Database.DATABASE.users;
		List<User> patients = new ArrayList<User>();
		for (User u : users) {
			SystemUser u2 = (SystemUser) u;
			if (u2.getNameFirst().toLowerCase().contains(s.toLowerCase()) || 
				u2.getNameMiddle().toLowerCase().contains(s.toLowerCase()) ||
				u2.getNameLast().toLowerCase().contains(s.toLowerCase())) {
				patients.add(u);
			}
		}
		return patients;
	}
	
	/**
	 * Method setActive.
	 * @param id Integer
	 * @param active Boolean
	 */
	public void setActive(Integer id, Boolean active) {
		SystemUser user = (SystemUser) getUser(id);
		user.setActive(active);
		save();
	}
	
	/**
	 * super's toString
	
	 * @return super's toString */
	public String toString() {
		return super.toString();
	}
	
	/**
	 * Method save.
	 */
	public void save() {
		Database.DATABASE.saveUsers();
	}
}
